var customerId;

/**
 * @param {Object}
 *            id 弹出编辑对话框
 */
function delDataDialog(id) {
    $('#delCustomerDialog').modal('show');
    customerId = id;
}

$('#delButton').on('click', function () {

    $.ajax({
        // 请求方式
        type: "DELETE",
        // 请求地址
        url: "/customer?id=" + customerId,
        // 请求成功
        // contentType: 'application/json;charset=UTF-8',
        // // 数据
        // data: JSON.stringify(b),
        // // 请求成功
        success: function (result) {
            $('#delCustomerDialog').modal('hide');
            refreshData();
        },
        // 请求失败，包含具体的错误信息
        error: function (e) {
            console.log(e.status);
            console.log(e.responseText);
            alert("操作有误请联系管理员")
        }
    });
});

/**
 * @param {Object}
 *            id 弹出添加对话框
 */
function addDataDialog() {
    $('#addCustomerDialog').modal('show');

}

//restful规范： post  delete put  get  增删查改
function addData() {
    var custName = $("input[name=custName]")[0].value;
    var custIndustry = $("select[name=custIndustry]")[0].value;
    var custMobile = $("input[name=custMobile]")[0].value;
    var custLevel = $("select[name=custLevel]")[0].value;
    var custCreateTime = $("input[name=custCreateTime]")[0].value;
    var formData = new FormData();
    formData.append("file",$('#file')[0].files[0])
    formData.append("custName",custName)
    formData.append("custIndustry",custIndustry)
    formData.append("custMobile",custMobile)
    formData.append("custLevel",custLevel)
    formData.append('custCreateTime',custCreateTime)
    console.log("custId='"+customerId+"' custName='"+custName+"' custIndustry='"+custIndustry+"' custMobile='"+custMobile+"' custLevel='"+custLevel+"'")
    $.ajax({
        // 请求方式
        type: "POST",
        // 请求地址
        url: "/customer/addCustomer",
        // contentType: 'application/json;charset=UTF-8',
        // 数据
        data: formData,
        processData : false,
        contentType : false,
        // 请求成功
        success: function (result) {
            $('#addCustomerDialog').modal('hide');
            refreshData();
            console.log(result)
        },
        // 请求失败，包含具体的错误信息
        error: function (e) {
            console.log(e.status);
            console.log(e.responseText);
            alert("操作有误请联系管理员")
        }
    });
}

function showImg(cell) {
    baseImg= window.URL.createObjectURL(cell.files[0]);
    console.log("baseImg:"+baseImg)
}
function image2Base64(img) {//转base64的方法
    var canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;
    var ctx = canvas.getContext("2d");
    ctx.drawImage(img, 0, 0, img.width, img.height);
    var dataURL = canvas.toDataURL("image/png");//规定图片是什么格式，image/格式
    return dataURL;
}


function queryImg(data) {
//前端获取
    $('#queryCustomerDialog').modal('show');
    $('#customerList tbody').on('click', 'tr', function () {
        // console.log(table.row(this).data);
        $('#custName').text(data.custName);
        $('#custIndustry').text(data.custIndustry);
        $('#custLevel').text(data.custLevel);
        $('#custMobile').text(data.custMobile);
        //为<img>标签添加src，用来显示客户照片
        $('#custPhoto').attr("src",data.custPhoto)
    } );
}


//restful规范： post  delete put  get  增删查改
function modifyData() {
    var custName = $("input[name=custName1]").val();
    var custIndustry = $("select[name=custIndustry1]").val();
    var custMobile = $("input[name=custMobile1]").val();
    var custLevel = $("select[name=custLevel1]").val();
    var custCreateTime = $("input[name=custCreateTime1]").val();
// json串{key：value}
    var a = {
        'custId':customerId,
        'custName': custName,
        'custIndustry': custIndustry,
        'custMobile': custMobile,
        'custLevel': custLevel,
        'custCreateTime': custCreateTime
    };
// jquery ajax用法
    $.ajax({
        // 请求方式
        type: "PUT",
        // 请求地址
        url: "/customer",
        // 数据
        data: JSON.stringify(a),

        contentType: 'application/json;charset=UTF-8',
        // 请求成功
        success: function (result) {
            $('#modifyCustomerDialog').modal('hide');
            refreshData();

        },
        // 请求失败，包含具体的错误信息
        error: function (e) {
            console.log(e.status);
            console.log(e.responseText);
            alert("操作有误请联系管理员")
        }
    });
}

function modifyDataDialog(customer) {
    $('#modifyCustomerDialog').modal('show');
    customerId=customer.custId;
    $("input[name=custName1]").val(customer.custName);

    if(customer.custIndustry=="教育培训"){
        $("select[name=custIndustry1]").val(1);
    }
    else if(customer.custIndustry=="电子商务"){
        $("select[name=custIndustry1]").val(2);
    }
    else if(customer.custIndustry=="对外贸易"){
        $("select[name=custIndustry1]").val(3);
    }
    else if(customer.custIndustry=="酒店旅游"){
        $("select[name=custIndustry1]").val(4);
    }
    else if(customer.custIndustry=="房地产"){
        $("select[name=custIndustry1]").val(5);
    }else{
        $("select[name=custIndustry1]").val(0);
    }


    if(customer.custLevel=="VIP客户"){
        $("select[name=custLevel1]").val(6);
    }
    else if(customer.custLevel=="普通客户"){
        $("select[name=custLevel1]").val(7);
    }else{
        $("select[name=custLevel1]").val(0);
    }

    $("input[name=custMobile1]").val(customer.custMobile);

    $("input[name=custCreateTime1]").val(customer.custCreateTime);


}

//注销
function logout() {
    $.ajax({
        // 请求方式 请求方法
        type : "Post",
        // 请求地址
        url : "/logout",

        // 请求成功  回调函数
        success : function(result) {
            // alert(JSON.stringify(response))
            // document.body.append("\n"+JSON.stringify(response));
            window.location.href = "index.html";
        },
        // 请求失败，包含具体的错误信息
        error : function(e) {
            console.log(e.status);
            console.log(e.responseText);
            alert("操作有误请联系管理员");
        }
    });
}

function refreshData() {
    var b = {
        'custId': $("#selectId").val() != "" ? $("#selectId").val() : 0,
        'custName': $("#selectName").val(),
        'custIndustry': $("#selectIndustry").val(),
        'custMobile': $("#selectMobile").val(),
        'custLevel': $("#selectLevel").val(),
        'custPhoto':$('#selectPhoto').val(),
        "limit":1,
        "page":1
    };
    //1、先发送请求先去取count，然后把结果返回到前端供翻页提高效率使用
    $.ajax({
        // 请求方式
        type: "post",
        // 请求地址
        url: "/customer/count",
        data: JSON.stringify(b),
        contentType: 'application/json;charset=UTF-8',
        // 请求成功
        success: function (result) {
            b.recordsTotal=result;
            getData(b);

        },
        // 请求失败，包含具体的错误信息
        error: function (e) {
            console.log(e.status);
            console.log(e.responseText);
            alert("操作有误请联系管理员")
        }
    });
}

function getData(query) {

    $('#customerList')
        .DataTable(
            {
                "destroy": true,
                "searching":false,
                "pagingType": "full_numbers",
                "sLoadingRecords": "正在加载数据...",
                "sZeroRecords": "暂无数据",
                "ajax": {
                    url: "/customer/query",
                    dataSrc: 'data',//原来为空，修改为data 因为服务端模式返回的是data入参
                    type:"post",
                    data : query,
                },
                "serverSide": true,//服务端分页
                "lengthChange": false,//是否允许用户自定义显示数量
                "scrollY": "360px",//竖向滚动条
                "scrollCollapse": "true",//是否出现滚动条
                'iDisplayLength':50,
                // "searching": true,
                "columns": [
                    {
                        "data": "custId"
                    },
                    {
                        "data": "custName"
                    },
                    {
                        "data": "custIndustry"
                    },
                    {
                        "data": "custLevel"
                    },
                    {
                        "data": "custMobile"
                    },
                    {
                        "data": "custCreateTime"
                    },
                    {
                        "data": null,
                        "render": function (data, type, row, meta) {
                           var obj = JSON.stringify(data).replace(/\"/g,"'");
                            var html = "<a  href='#' role='button' data-toggle='modal' onclick='modifyDataDialog("
                                + JSON.stringify(row)
                                + ")'><i class='fa fa-pencil' ></i>修改</a>    "
                                + '<a href="#" role="button" data-toggle="modal"  onclick="delDataDialog('
                                + row.custId
                                + ')"><i class="fa fa-trash-o"  ></i>删除</a>  '
                                + '<a href="#" role="button" data-toggle="modal"  onclick="queryImg('
                                +obj
                                + ')"><i class="fa fa-trash-o"  ></i>查看</a>';
                            return html;
                        }
                    }
                ],
                "language": {
                    "processing": "玩命加载中...",
                    "lengthMenu": "显示 _MENU_ 项结果",
                    "zeroRecords": "没有匹配结果",
                    "info": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
                    "infoEmpty": "显示第 0 至 0 项结果，共 0 项",
                    "infoFiltered": "(由 _MAX_ 项结果过滤)",
                    "infoPostFix": "",
                    "url": "",
                    "paginate": {
                        "first": "首页",
                        "previous": "前一页",
                        "next": "下一页",
                        "last": "末页"
                    }
                }
            });

}
